/**
 * 
 */
package com.util;

import javax.swing.text.PlainDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.AttributeSet;

/**
 * 
 * @author Taylor
 * @version 1.0
 */
public class JTextFieldLimit extends PlainDocument {
	/**
	 * Generated serialVersionUID
	 */
	private static final long serialVersionUID = -5910813575837018003L;
	
	/**
	 * 
	 */
	private final int limit;
	
	/**
	 * 
	 */
	private boolean toUppercase = false;

	/**
	 * 
	 * @param limit
	 */
	public JTextFieldLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * 
	 * @param limit
	 * @param upper
	 */
	public JTextFieldLimit(int limit, boolean upper) {
		this.limit = limit;
		toUppercase = upper;
	}

	/**
	 * 
	 * @param offset
	 * @param str
	 * @param attr
	 * @throws BadLocationException
	 */
	public void insertString(int offset, String str, AttributeSet attr)
			throws BadLocationException {
		if (str == null){
			return;
		}

		if ((getLength() + str.length()) <= limit) {
			if (toUppercase){
				str = str.toUpperCase();
			}
			super.insertString(offset, str, attr);
		}
	}
	
	/**
	 * @return nonsense
	 */
	public String toString(){
		return "Why the fuck would you do this?";
	}
}